﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
using NLog;
using OddUserManager.Model;
using OddUserManager.Service.IServices;
using OddUserManager.Web.Models;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace OddUserManager.Web.Controllers
{
    public class AnalystSelectedMatchController : Controller
    {
        static Logger log = LogManager.GetCurrentClassLogger();
        private IAnalystSelectedMatchService _service;

        public AnalystSelectedMatchController(IAnalystSelectedMatchService service) {
            _service = service;
        }

        public async Task<IActionResult> GetAnalystSelectedMatchsByMatchIdsAndUserId(List<int> matchIds)
        {
            JsonData data = new JsonData();
            var result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            int id = 0;
            if (result.Succeeded)
            {
                id = int.Parse(result.Principal.Identity.Name);
                data.Data = _service.GetAnalystSelectedMatchsByMatchIdsAndUserId(matchIds, id);
                data.Success = true;
            }
            else {
                data.Message = "用户凭证失效,请重新登陆";
                data.Success = false;
            }

            return Json(data);
        }

        public async Task<IActionResult> GetAnalystSelectedMatchsByMatchIds(List<int> matchIds)
        {
            JsonData data = new JsonData();
            var result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            int id = 0;
            if (result.Succeeded)
            {
                id = int.Parse(result.Principal.Identity.Name);
                data.Data = _service.GetAnalystSelectedMatchsByMatchIds(matchIds);
                data.Success = true;
            }
            else
            {
                data.Message = "用户凭证失效,请重新登陆";
                data.Success = false;
            }

            return Json(data);
        }



        public async Task<IActionResult> Edit(List<AnalystSelectedMatch> matchs,List<int> ids)
        {
            JsonData d = new JsonData();
            try {
                if (null != matchs && matchs.Count > 0)
                {
                    var result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
                    int id = 0;
                    if (result.Succeeded)
                    {
                        id = int.Parse(result.Principal.Identity.Name);
                    }
                    else
                    {
                        throw new Exception("登陆凭证已经失效");
                    }
                    var oldMatchs = _service.GetAnalystSelectedMatchsByMatchIdsAndUserId(ids, id);
                    var newIds = matchs.Select(p => p.MatchId).ToList();

                    var deleteMatchs = oldMatchs.Where(p => !newIds.Contains(p.MatchId)).ToList();

                    deleteMatchs.ForEach(p =>
                    {
                        _service.Delete(p.Id);
                    });
                    matchs.ForEach(p =>
                    {
                        p.CreateAppUserId = id;
                        p.CreateTime = DateTime.Now;
                        p.LastUpdateTime = DateTime.Now;
                        _service.InsertOrUpdateAnalystSelectedMatch(p);
                    });
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                d.Success = false;
                d.Message = d.Message;
            }
            return Json(d);
        }
    }
}
